package cn.bugDance.trigger.http;
import cn.bugDance.api.strategy.controller.IRaffleStrategyController;
import cn.bugDance.api.strategy.dto.RaffleStrategyAwardListResponseDTO;
import cn.bugDance.api.strategy.dto.RandomRaffleRequestDTO;
import cn.bugDance.api.strategy.dto.RandomRaffleResponseDTO;
import cn.bugDance.domain.activity.service.armory.IRaffleActivityArmory;
import cn.bugDance.domain.strategy.model.entity.RaffleAwardEntity;
import cn.bugDance.domain.strategy.model.entity.RaffleFactorEntity;
import cn.bugDance.domain.strategy.model.entity.StrategyAwardEntity;
import cn.bugDance.domain.strategy.service.IRaffleStrategyService;
import cn.bugDance.domain.strategy.service.armory.IRaffleStrategyArmory;
import cn.bugDance.types.enums.ResponseCode;
import cn.bugDance.types.response.Response;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@RestController()
@RequestMapping("/raffle/${app.config.api-version}/strategy")
@CrossOrigin("${app.config.cross-origin}")
@Slf4j
public class StrategyController implements IRaffleStrategyController {

    @Resource
    private IRaffleStrategyService strategyService;
    @Resource
    private IRaffleStrategyArmory raffleStrategyArmory;
    @Resource
    private IRaffleActivityArmory activityArmory;

    private DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");


    @Override
    @RequestMapping(value = "/query_raffle_award_list", method = RequestMethod.GET)
    public Response<List<RaffleStrategyAwardListResponseDTO>> queryRaffleStrategyAwardList(@RequestParam Long strategyId) {
        try{
            log.info("【策略控制层-StrategyController】: 前端获取策略奖品列表: strategyId: {}", strategyId);

            List<StrategyAwardEntity> strategyAwardEntities = strategyService.queryStrategyAwardList(strategyId);
            List<RaffleStrategyAwardListResponseDTO> responseDTOList = new ArrayList<>();

            for (StrategyAwardEntity strategyAwardEntity : strategyAwardEntities) {
                if(strategyAwardEntity.getRaffle() == 0){
                    RaffleStrategyAwardListResponseDTO raffleStrategyAwardListResponseDTO = new RaffleStrategyAwardListResponseDTO();

                    raffleStrategyAwardListResponseDTO.setAwardId(strategyAwardEntity.getAwardId());
                    raffleStrategyAwardListResponseDTO.setAwardTitle(strategyAwardEntity.getAwardTitle());
                    raffleStrategyAwardListResponseDTO.setAwardSubTitle(strategyAwardEntity.getAwardSubTitle());
                    raffleStrategyAwardListResponseDTO.setSort(strategyAwardEntity.getSort());

                    responseDTOList.add(raffleStrategyAwardListResponseDTO);
                }
            }

            return Response.<List<RaffleStrategyAwardListResponseDTO>>builder()
                    .code(ResponseCode.SUCCESS.getCode())
                    .info(ResponseCode.SUCCESS.getInfo())
                    .data(responseDTOList).build();
        }catch (Exception e){
            log.warn("【策略控制层-StrategyController】: 前端获取奖品列表失败: strategyId: {}", strategyId);
            return Response.<List<RaffleStrategyAwardListResponseDTO>>builder()
                    .code(ResponseCode.UN_ERROR.getCode())
                    .info(ResponseCode.UN_ERROR.getInfo()).build();
        }
    }


    @Override
    @RequestMapping(value = "/random_raffle", method = RequestMethod.POST)
    public Response<RandomRaffleResponseDTO> randomRaffle(@RequestBody RandomRaffleRequestDTO randomRaffleRequestDTO) {
        try{
            log.info("【策略控制层-StrategyController】: 前端随机抽奖: strategyId: {}, userId: {}", randomRaffleRequestDTO.getStrategyId(), randomRaffleRequestDTO.getUserId());

            RaffleFactorEntity raffleFactorEntity = new RaffleFactorEntity();
            raffleFactorEntity.setStrategyId(randomRaffleRequestDTO.getStrategyId());
            raffleFactorEntity.setUserId(randomRaffleRequestDTO.getUserId());
            Date endTime = activityArmory.queryActivityEndTime(100301L);
            raffleFactorEntity.setEndDateTime(endTime);
            raffleFactorEntity.setDay(dateFormat.format(new Date()));
            raffleFactorEntity.setActivityId(100301L);

            RaffleAwardEntity raffleAwardEntity = strategyService.performRaffle(raffleFactorEntity);
            log.info("【策略控制层-StrategyController】: 前端随机抽奖结果: strategyId: {}, awardId: {}", randomRaffleRequestDTO.getStrategyId(), raffleAwardEntity.getAwardId());

            RandomRaffleResponseDTO responseDTO = new RandomRaffleResponseDTO();
            responseDTO.setAwardId(raffleAwardEntity.getAwardId());
            responseDTO.setSort(raffleAwardEntity.getSort());
            responseDTO.setAwardTitle(raffleAwardEntity.getAwardTitle());

            return Response.<RandomRaffleResponseDTO>builder()
                    .code(ResponseCode.SUCCESS.getCode())
                    .info(ResponseCode.SUCCESS.getInfo())
                    .data(responseDTO).build();

        }catch (Exception e){
            log.warn("【策略控制层-StrategyController】: 前端随机抽奖失败: strategyId: {}", randomRaffleRequestDTO.getStrategyId());

            return Response.<RandomRaffleResponseDTO>builder()
                    .code(ResponseCode.UN_ERROR.getCode())
                    .info(ResponseCode.UN_ERROR.getInfo()).build();
        }
    }


    @Override
    @RequestMapping(value = "/strategy_assemble", method = RequestMethod.GET)
    public Response<String> strategyAssemble(@RequestParam Long activityId) {
        try{
            //raffleStrategyArmory.assembleRaffleStrategy(strategyId);
            Long strategyId = activityArmory.assembleRaffleActivity(activityId);
            Date endTime = activityArmory.queryActivityEndTime(activityId);
            raffleStrategyArmory.assembleRaffleStrategy(strategyId, endTime);
            return Response.<String>builder()
                    .code(ResponseCode.SUCCESS.getCode())
                    .info(ResponseCode.SUCCESS.getInfo()).build();
        }catch (Exception e){
            return Response.<String>builder()
                    .code(ResponseCode.UN_ERROR.getCode())
                    .info(ResponseCode.UN_ERROR.getInfo()).build();
        }
    }
}
